﻿#include "pingpong_private.h"
#include "server_qt.h"
#include "client_qt.h"
#include "server_asio.h"
#include "client_asio.h"

using namespace std;

PingpongPrivate::PingpongPrivate() {
}

PingpongPrivate::~PingpongPrivate() {
}

void PingpongPrivate::Start(NetworkType client, NetworkType server) {
    thread_ = thread([this, client, server]() {
        // 选择服务端
        if (server == kAsio) {
            pingpong_server_.reset(new ServerAsio);
        } else if (server == kQt) {
            pingpong_server_.reset(new ServerQt);
        }
        // 选择客户端
        if (client == kAsio) {
            pingpong_client_.reset(new ClientAsio);
        } else if (client == kQt) {
            pingpong_client_.reset(new ClientQt);
        }
        application_ = make_unique<QCoreApplication>(argc, nullptr);
        pingpong_server_->Start();
        pingpong_client_->Start(pingpong_server_->Port(), "hello wordl");
        application_->exec();
        application_.reset(nullptr);
    });
}

void PingpongPrivate::Stop() {
    pingpong_server_->Stop();
    pingpong_client_->Stop();
    application_->exit();
    pingpong_server_->Wait();
    pingpong_client_->Wait();
    thread_.join();
}

unsigned long long PingpongPrivate::Result() {
    return pingpong_client_->count();
}